package command;

import java.net.InetAddress;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.*;


import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CommonUtil;
import common.Crypto;
import common.EDUBOX_Baese64_Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class AdminCommand extends Command {

	private String next;
	
	public AdminCommand(String next) {
		this.next=next;
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		HashMap listMap = new HashMap();
		
		Connection con = null;  
		try {
			
			
			HttpSession session = req.getSession();
			UserSession userSession = (UserSession)session.getAttribute("user"); 

			String login_id = null;
			String user_id = null;
			String level = null;
			String admin_ent_id = null;
			
			user_id = CommonUtil.nvl(req.getParameter("user_id"));
			level = CommonUtil.nvl(req.getParameter("level"));
			admin_ent_id = CommonUtil.nvl(req.getParameter("admin_ent_id"));

			if(userSession!= null){
				login_id = userSession.getLogin_id();
				
				
				if(user_id.equals("")){
					user_id = userSession.getUser_id();
				}
				
				if(level.equals("") ){
					level = userSession.getAdmin_level();
				}
				
				if(admin_ent_id.equals("") ){
					admin_ent_id = userSession.getEnt_id();
				}
				
			}
			
			System.out.println("user_id====>" + user_id);
			System.out.println("level====>" + level);
			System.out.println("admin_ent_id====>" + admin_ent_id);
			
			String search_combo = CommonUtil.nvl(req.getParameter("search_combo"));
			String keyword = CommonUtil.nvl(req.getParameter("keyword"));
			String line_number = CommonUtil.nvl(req.getParameter("line_number"),"10");
			String total_count = CommonUtil.nvl(req.getParameter("total_count"),"0");
			String curr_page = CommonUtil.nvl(req.getParameter("curr_page"),"1");
			
			if(search_combo.equals("2")){
				keyword = CommonUtil.nvl(Crypto.EncodeBySType(keyword));
				System.out.println("keyword====>" + keyword);
			}
			
						
			con = (new CommonConnect()).getConnection_ds1();
			con.setAutoCommit(false);
			String[] param=null;
			String queryStr = QueryManager.getQuery("admin_list.query");
			param= new String[8];
			param[0] = user_id;	
			param[1] = search_combo;	
			param[2] = keyword;	
			param[3] = line_number;	
			param[4] = total_count;	
			param[5] = curr_page;	
			param[6] = level;
			param[7] = admin_ent_id;
			
			
			list = DAO.select(con,queryStr,param);
			
			listMap.put("mainContent",list);
			logger.debug("queryStr ==> "+"admin_list.query");		     
		     
// 리퀘스트에 모두 담는다.
			req.setAttribute("list_map",listMap);

	

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
